/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.parts;

import com.eng.scc.core.util.ImageFinder;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Rafael Danieli
 */
public abstract class ItemSelector<T> extends JPanel implements Selector
{
    private List<ChangeListener> listeners = new ArrayList<ChangeListener>();
    private T source = null;

    public T getSource()
    {
        return source;
    }

    public void setSource( T source )
    {
        this.source = source;
        
        if ( source != null )
        {
            tfValue.setText( source.toString() );
        }
        
        else
        {
            tfValue.setText( "" );
        }
        
        fireStateChanged();
    }

    @Override
    public void clear()
    {
        setSource( null );
        tfValue.setText( "" );
        fireStateChanged();
    }

    public void setEditable( boolean b )
    {
        tfValue.setEditable( b );
    }
    
    public ItemSelector()
    {
        initComponents();
    }

    public void setImages( String pathChoose, String pathClear )
    {
        buttonChoose.setIcon( ImageFinder.getImageIcon( pathChoose ) );
        buttonClear.setIcon( ImageFinder.getImageIcon( pathClear ) );
    }
    
    public void setEnable( boolean value )
    {
        tfValue.setEnabled( value );
    }
    
    private void initComponents()
    {
        setLayout( new GridBagLayout() );
        
        add( tfValue, new GridBagConstraints( 0, 0, 1, 1, 1.0, 0.0,
                                        GridBagConstraints.WEST, 
                                        GridBagConstraints.HORIZONTAL, 
                                        new Insets( 5, 0, 5, 5 ), 0, 0 ));
        
        add( buttonChoose, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0,
                                        GridBagConstraints.WEST, 
                                        GridBagConstraints.NONE, 
                                        new Insets( 5, 1, 5, 5 ), 0, 0 ));
        
        add( buttonClear, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0,
                                        GridBagConstraints.WEST, 
                                        GridBagConstraints.NONE, 
                                        new Insets( 5, 1, 5, 0 ), 0, 0 ));
        
        setImages( "bt_folder.png", "bt_clear.png" );
        
        buttonChoose.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        buttonClear.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        
        buttonChoose.setOpaque(false);
        buttonChoose.setContentAreaFilled(false);
        buttonChoose.setBorderPainted(false);
        buttonChoose.setFocusable( false);
        
        buttonClear.setOpaque(false);
        buttonClear.setContentAreaFilled(false);
        buttonClear.setBorderPainted(false);
        buttonClear.setFocusable( false);
        
        buttonChoose.setPreferredSize( new Dimension( 20 , 20 ) );
        buttonClear.setPreferredSize( new Dimension( 20 , 20 ) );
        
        buttonChoose.addActionListener( new ActionListener() {

            @Override
            public void actionPerformed( ActionEvent e )
            {
                chooseItem();
            }
        });
        
        buttonClear.addActionListener( new ActionListener() {

            @Override
            public void actionPerformed( ActionEvent e )
            {
                clear();
            }
        });
    }
    
    private JTextField tfValue = new JTextField();
    private JButton buttonChoose = new JButton();
    private JButton buttonClear = new JButton();
    
    public void addChangeListener( ChangeListener changeListener )
    {
        if ( changeListener != null )
        {
            listeners.add( changeListener );
        }
    }
    
    private void fireStateChanged()
    {
        for ( ChangeListener changeListener : listeners )
        {
            changeListener.stateChanged( null );
        }
    }
}
